//
//  ListCell.m
//
//  Created by lee jory on 09-10-22.
//  Copyright 2009 Netgen. All rights reserved.
//

#import "ListCell.h"
#import "MCFunction.h"
#import "MCUIToolkit.h"
#import "EGORefreshTableHeaderView.h"

@implementation DataGridComponentDataSource
@synthesize titles,data,fieldTypes,columnWidth;
@end

@implementation DataGridScrollView
@synthesize listCell;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if (self.decelerating) {
		return;
	}
	UITouch *t = [touches anyObject];
	if([t tapCount] == 1){
		ListCell *d = (ListCell*)listCell;
		int idx = [t locationInView:self].y / d.cellHeight;
		index = idx;
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationDuration:0.65];
		[UIView setAnimationDelegate:self];
		[UIView setAnimationWillStartSelector:@selector(doAction)];
		for(int i = 0; i < [d.dataSource.titles count]; i++){
			UILabel *l = (UILabel*)[listCell viewWithTag:idx * d.cellHeight + i + 1000];
			UILabel *lSpaceLeft = (UILabel*)[listCell viewWithTag:idx * d.cellHeight + i + 10000];
			UILabel *lSpaceRight = (UILabel*)[listCell viewWithTag:idx * d.cellHeight + i + 20000];
			l.alpha = .5;
			lSpaceLeft.alpha = .5;
			lSpaceRight.alpha = .5;
		}
		UIButton *selectButton = (UIButton *)[listCell viewWithTag:index + 100];
		if (selectButton) {
			selectButton.alpha = .5;
		}
		for(int i = 0; i < [d.dataSource.titles count]; i++){
			UILabel *l = (UILabel*)[listCell viewWithTag:idx * d.cellHeight + i + 1000];
			UILabel *lSpaceLeft = (UILabel*)[listCell viewWithTag:idx * d.cellHeight + i + 10000];
			UILabel *lSpaceRight = (UILabel*)[listCell viewWithTag:idx * d.cellHeight + i + 20000];
			l.alpha = 1.0;
			lSpaceLeft.alpha = 1.0;
			lSpaceRight.alpha = 1.0;
		}		
		if (selectButton) {
			selectButton.alpha = 1.0;
		}
		[UIView commitAnimations];
	}
}

- (void)doAction
{
	[listCell doAction:index];
}

@end



@interface ListCell (Private)
//int actionSheetFlag;
//初始化各子视图
-(void)layoutSubView:(CGRect)aRect;
//用数据项填冲数据
-(void)fillData;
@end
@implementation ListCell
@synthesize vRight, vLeft, cellHeight, dataSource;
@synthesize dataObjectName;
@synthesize layoutName;
@synthesize listData;
@synthesize listLayout;
@synthesize gridType;
@synthesize tabIndex;
@synthesize viewController;
@synthesize valueBufferArray;

- (float)cellTitleHeight
{
	return 40.0;
}

-(void)addRightArrow:(UIView*)view
{
}

- (id)initWithFrame:(CGRect)aRect 
			   data:(DataGridComponentDataSource*)aDataSource 
		   gridType:(NSString *)aGridType
		  totalPage:(int)totalPg 
		currentPage:(int)curPage
 isFirstColumnFixed:(bool)aIsFirstColumnFixed
			 isMsel:(bool)aIsMsel
{
	self = [super initWithFrame:aRect];
	if(self != nil){
		self.clipsToBounds = YES;
		self.gridType = aGridType;
		if ([self.gridType isEqualToString:@"S"]) {
			self.backgroundColor = [UIColor colorWithPatternImage:[MCUserDefaults instance].skin_bgViewImg];//[UIColor colorWithRed:14.5 green:56.1 blue:76.5 alpha:1];
		}else {
			self.backgroundColor = [UIColor clearColor];
		}
		
		self.dataSource = aDataSource;
		//初始显示视图及Cell的长宽高
		contentWidth = 0;
		cellHeight = 40.0;
		
		isFirstColumnFixed = aIsFirstColumnFixed;
		isMsel = aIsMsel;
		if (isFirstColumnFixed) {
			cellWidth = [[dataSource.columnWidth objectAtIndex:0] floatValue];
			startIndex = 1;
			if (CGRectGetWidth(aRect) < CGRectGetHeight(aRect) && cellWidth > CGRectGetWidth(aRect)/2) {
				cellWidth = CGRectGetWidth(aRect)/2;
			}
			if (CGRectGetWidth(aRect) > CGRectGetHeight(aRect) && cellWidth > CGRectGetWidth(aRect)/3) {
				cellWidth = CGRectGetWidth(aRect)/3;
			}
		} else if (isMsel) {
			cellWidth = [[dataSource.columnWidth objectAtIndex:0] floatValue];
			startIndex = 1;
		} else {
			cellWidth = 0;
			startIndex = 0;
		}
	
		for(int i = startIndex;i<[dataSource.columnWidth count];i++) {
			contentWidth += [[dataSource.columnWidth objectAtIndex:i] floatValue];
		}
		int count = [dataSource.columnWidth count];
		int otherCount = count;
		if (isFirstColumnFixed || isMsel) {
			otherCount = count - 1;
		}
		if (contentWidth + cellWidth < CGRectGetWidth(aRect) && otherCount > 0) {
			float other = (int)(CGRectGetWidth(aRect) - contentWidth - cellWidth) % otherCount;
			float addition = (CGRectGetWidth(aRect) - contentWidth - cellWidth - other) / otherCount;
			for(int i = startIndex;i < count;i++) {
				int columnWidth = [[dataSource.columnWidth objectAtIndex:i] intValue];
				if (i == count - 1) {
					[dataSource.columnWidth replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%f", columnWidth+addition+other]];
				}else {
					[dataSource.columnWidth replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%f", columnWidth+addition]];
				}
			}
			contentWidth = CGRectGetWidth(aRect)-cellWidth;
		}
		contentHeight = [dataSource.data count] * cellHeight;
		
		totalPage = totalPg;
		page = curPage;
		//初始化各视图
		[self layoutSubView:aRect];
		//填冲数据
		[self fillData];
	}
	return self;
}
-(void)layoutSubView:(CGRect)aRect{
	vLeftContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, contentHeight)];
	vRightContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, aRect.size.width - cellWidth, contentHeight)];
	vLeftContent.backgroundColor = [UIColor colorWithRed:0.667 green:0.722 blue:0.788 alpha:1.000];
	vRightContent.backgroundColor = [UIColor colorWithRed:0.667 green:0.722 blue:0.788 alpha:1.000];
	
	vLeftContent.opaque = YES;
	vRightContent.opaque = YES;
	
	vTopLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, self.cellTitleHeight)];
	vTopRight = [[UIView alloc] initWithFrame:CGRectMake(cellWidth, 0, aRect.size.width - cellWidth, self.cellTitleHeight)];
	
	vLeft = [[DataGridScrollView alloc] initWithFrame:CGRectMake(0, self.cellTitleHeight, CGRectGetWidth(aRect), aRect.size.height - self.cellTitleHeight)];
	vRight = [[DataGridScrollView alloc] initWithFrame:CGRectMake(cellWidth, 0, CGRectGetWidth(aRect) - cellWidth, contentHeight)];
	
	vLeft.backgroundColor = [UIColor colorWithRed:0.667 green:0.722 blue:0.788 alpha:1.000];
	
	vLeft.listCell = self;
	vRight.listCell = self;
	
	vLeft.opaque = YES;
	vRight.opaque = YES;
	vTopLeft.opaque = YES;
	vTopRight.opaque = YES;
	
	//设置ScrollView的显示内容
	vLeft.contentSize = CGSizeMake(CGRectGetWidth(aRect), contentHeight);
	vRight.contentSize = CGSizeMake(contentWidth, aRect.size.height - self.cellTitleHeight);
	
	//设置ScrollView参数
	vRight.delegate = self;
	vLeft.delegate = self;
	MCUserDefaults *defaults = [MCUserDefaults instance];
	vTopRight.backgroundColor = defaults.skin_bgListLabelSpaceColor;	
	vTopLeft.backgroundColor = defaults.skin_bgListLabelSpaceColor;
	
	vRight.backgroundColor = [UIColor clearColor];
	vLeft.backgroundColor = [UIColor clearColor];
	
	//添加各视图
	[vRight addSubview:vRightContent];
	[vRightContent release];
	[vLeft addSubview:vLeftContent];
	[vLeftContent release];
	[vLeft addSubview:vRight];
	[vRight release];
	[self addSubview:vTopLeft];
	[vTopLeft release];
	[self addSubview:vLeft];
	[vLeft release];
	
	[vLeft bringSubviewToFront:vRight];
	[self addSubview:vTopRight];
	[vTopRight release];
	[self bringSubviewToFront:vTopLeft];
}

- (float)getListLabelFontSize {
	return 16.0;
}

-(void)fillData{
	float columnOffset = 0.0;
	float spaceWidth = 5.0;
	float lineWidth = 1.0;
	float lineHeight = 1.0;
	//填冲标题数据
	for(int column = 0; column < [dataSource.titles count]; column++){
		float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, 0, columnWidth - lineWidth, self.cellTitleHeight)];
		label.font = [UIFont systemFontOfSize:[self getListLabelFontSize]];
		NSArray *values = [[dataSource.titles objectAtIndex:column] componentsSeparatedByString:@":-:"];
		label.text = [values objectAtIndex:0];
		label.textColor = [UIColor whiteColor];
		if ([values count] > 1) {
			label.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [values objectAtIndex:1]]];
		}
		MCUserDefaults *defaults = [MCUserDefaults instance];
		label.backgroundColor = [UIColor colorWithPatternImage:defaults.skin_bgListLabelImg];
		label.textAlignment = UITextAlignmentCenter;

		if (isFirstColumnFixed && column == 0) {
			label.frame = CGRectMake(0, 0, cellWidth - lineWidth, self.cellTitleHeight);
			[vTopLeft addSubview:label];
		}else if (isMsel && column == 0) {
			UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
			button.frame = CGRectMake(0, 0, cellWidth - lineWidth, self.cellTitleHeight);
			button.tag = -1;
			button.backgroundColor = [UIColor colorWithPatternImage:defaults.skin_bgListLabelImg];
			[button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
			[button addTarget:self action:@selector(selectCell:) forControlEvents:UIControlEventTouchUpInside];
			[vTopLeft addSubview:button];
			//UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unselected.png"]];
			//image.frame = CGRectMake((cellWidth - lineWidth-self.cellTitleHeight)/2, 0, self.cellTitleHeight, self.cellTitleHeight);
			//[button addSubview:image];
			//[image release];
		} else {
			[vTopRight addSubview:label];
			columnOffset += columnWidth;
		}
		
		[label release];
	}	
		
	//填冲数据内容	
	MCUserDefaults *defaults = [MCUserDefaults instance];
	for(int i = 0;i<[dataSource.data count];i++){
		NSArray *rowData = [dataSource.data objectAtIndex:i];
		columnOffset = 0.0;
		for(int column = 0; column < [rowData count]; column++){
			if (isMsel) {
				if (column == 0) {
					float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
					UIButton *button = [[UIButton alloc] init];
					button.frame = CGRectMake(columnOffset, i * cellHeight, columnWidth - lineWidth, cellHeight - lineHeight);
					[button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
					if (i % 2) {
						button.backgroundColor = defaults.skin_bgTableEvenColor;
					}
					else {
						button.backgroundColor = defaults.skin_bgTableOddColor;
					}
					button.tag = i + 100;
					[button addTarget:self action:@selector(selectCell:) forControlEvents:UIControlEventTouchUpInside];
					
					[vLeftContent addSubview:button];	
					[button release];
					continue;
				}
			}
			float columnWidth = [[dataSource.columnWidth objectAtIndex:column] floatValue];
			NSString *fieldType = [dataSource.fieldTypes objectAtIndex:column];
			if ([fieldType isEqualToString:@"H"]
				||[fieldType isEqualToString:@"I"]
				||[fieldType isEqualToString:@"K"]
				||[fieldType isEqualToString:@"L"]) {
				NSArray *selectIcons = [[rowData objectAtIndex:column] componentsSeparatedByString:@"::"];
				if ([selectIcons count] > 1) {
					NSString *iconLabel = [selectIcons objectAtIndex:1];
					NSArray *icons = [iconLabel componentsSeparatedByString:@","];
					UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(columnOffset, i * cellHeight, columnWidth - lineWidth, cellHeight - lineHeight)];
					if (i % 2) {
						labelView.backgroundColor = defaults.skin_bgTableEvenColor;
					}
					else {
						labelView.backgroundColor = defaults.skin_bgTableOddColor;
					}
					labelView.tag = i * cellHeight + column + 1000;
					for (int j = 0; j < [icons count]; j++) {
						NSString *icon = [icons objectAtIndex:j];
						UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(28 * j + 5+([icons count]==1?(80-24)/2-5:0), 8, 24, 24)];
						imageView.image = [MCFunction scaleAndRotateImage:[UIImage imageNamed:icon] width:24.0 height:24.0];
						imageView.backgroundColor = [UIColor clearColor];
						[labelView addSubview:imageView];
						[imageView release];
					}
					if (isFirstColumnFixed && column == 0) {
						labelView.frame = CGRectMake(0, i * cellHeight, cellWidth, cellHeight - lineWidth);
						if (CGRectGetWidth(self.bounds) > CGRectGetHeight(self.bounds)) {
							labelView.frame = CGRectMake(0, i * cellHeight, cellWidth - lineWidth, cellHeight - lineHeight);
						}
						[vLeftContent addSubview:labelView];
					} else {
						[vRightContent addSubview:labelView];
					}

					[labelView release];
					columnOffset += columnWidth;
					continue;
				}
			}
			UILabel *labelSpaceLeft = [[UILabel alloc] init];
			UILabel *labelSpaceRight = [[UILabel alloc] init];
			UILabel *label = [[UILabel alloc] init];
			labelSpaceLeft.frame = CGRectMake(columnOffset, i * cellHeight, spaceWidth, cellHeight - lineHeight);
			label.frame = CGRectMake(columnOffset + spaceWidth, i * cellHeight, columnWidth - spaceWidth*2 - lineWidth, cellHeight - lineHeight);
			labelSpaceRight.frame = CGRectMake(columnOffset + columnWidth - spaceWidth - lineWidth, i * cellHeight, spaceWidth, cellHeight - lineHeight);
			if ([fieldType isEqualToString:@"6"]
				||[fieldType isEqualToString:@"7"]
				||[fieldType isEqualToString:@"E"]
				||[fieldType isEqualToString:@"1"]){
				label.textAlignment = UITextAlignmentRight;
			}else {
				label.textAlignment = UITextAlignmentLeft;
				label.numberOfLines = 0;
			}
			label.font = [UIFont systemFontOfSize:[self getListLabelFontSize]];
			[label setLineBreakMode:UILineBreakModeTailTruncation];
			
			NSArray *values = nil;
			NSObject* object = [rowData objectAtIndex:column];
			if ([object isKindOfClass:[NSString class]]) {
				values = [[rowData objectAtIndex:column] componentsSeparatedByString:@":-:"];
			}

			label.text = [values objectAtIndex:0];
			if ([values count] > 1) {
				label.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [values objectAtIndex:1]]];
			}
			if (i % 2) {
				label.backgroundColor = defaults.skin_bgTableEvenColor;
				labelSpaceLeft.backgroundColor = defaults.skin_bgTableEvenColor;
				labelSpaceRight.backgroundColor = defaults.skin_bgTableEvenColor;
			}
			else {
				label.backgroundColor = defaults.skin_bgTableOddColor;
				labelSpaceLeft.backgroundColor = defaults.skin_bgTableOddColor;
				labelSpaceRight.backgroundColor = defaults.skin_bgTableOddColor;
			}
			label.tag = i * cellHeight + column + 1000;
			labelSpaceLeft.tag = i * cellHeight + column + 10000;
			labelSpaceRight.tag = i * cellHeight + column + 20000;
			if (isFirstColumnFixed && column == 0) {
				labelSpaceLeft.frame = CGRectMake(0, i * cellHeight, spaceWidth, cellHeight - lineHeight);
				label.frame = CGRectMake(spaceWidth,  i * cellHeight , cellWidth - spaceWidth*2 - lineWidth, cellHeight - lineHeight);
				labelSpaceRight.frame = CGRectMake(cellWidth - spaceWidth - lineWidth, i * cellHeight, spaceWidth, cellHeight - lineHeight);
				[vLeftContent addSubview:labelSpaceLeft];
				[vLeftContent addSubview:label];
				[vLeftContent addSubview:labelSpaceRight];
				continue;
			} else {
				[vRightContent addSubview:labelSpaceLeft];
				[vRightContent addSubview:label];
				[vRightContent addSubview:labelSpaceRight];
			}
			columnOffset += columnWidth;

			if (column == [rowData count]-1 && column == 0) {
				[self addRightArrow:label];
			}
			[labelSpaceLeft release];
			[label release];
			[labelSpaceRight release];
		}
	}	
}


//-------------------------------以下为事件触发方法----------------------------------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
	if (scrollView == vRight) {
		vTopRight.frame = CGRectMake(cellWidth, 0, vRight.contentSize.width, vTopRight.frame.size.height);
		vTopRight.bounds = CGRectMake(scrollView.contentOffset.x, 0, vTopRight.frame.size.width, vTopRight.frame.size.height);
		vTopRight.clipsToBounds = YES;	
		vRightContent.frame = CGRectMake(0, 0,vRight.contentSize.width, contentHeight);
		[self addSubview:vTopRight];
		[self bringSubviewToFront:vTopLeft];
		vRight.frame =CGRectMake(cellWidth, 0, self.frame.size.width - cellWidth, vLeft.contentSize.height); 
		[vLeft addSubview:scrollView];
	}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
	if (scrollView == vRight) {
		scrollView.frame = CGRectMake(cellWidth, 0, scrollView.frame.size.width, self.frame.size.height);
		vRightContent.frame = CGRectMake(0, self.cellTitleHeight - vLeft.contentOffset.y, vRight.contentSize.width , contentHeight);
		vTopRight.frame = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
		vTopRight.bounds = CGRectMake(0, 0, vRight.contentSize.width, vTopRight.frame.size.height);
		[scrollView addSubview:vTopRight];
		[self addSubview:scrollView];
	}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	if(!decelerate)
		[self scrollViewDidEndDecelerating:scrollView];
	if (totalPage > 1 && page < totalPage) {
		if (refreshHeaderView == nil && scrollView == vLeft) {
			CGRect frame = CGRectMake(0.0f, vLeft.contentSize.height, vLeft.bounds.size.width, 65);
			refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:frame withPage:page withTotlePage:totalPage withFlag:YES];
			refreshHeaderView.backgroundColor = [UIColor clearColor];
			[vLeft addSubview:refreshHeaderView];
			[refreshHeaderView release];
		}
	}
	int nScrollHeight = scrollView.bounds.size.height;
	if (scrollView == vLeft  && !_reloading && refreshHeaderView &&
		scrollView.contentOffset.y+nScrollHeight >= scrollView.contentSize.height+ 65.0f &&
		[viewController respondsToSelector:@selector(getNextData)] &&
		[viewController performSelector:@selector(getNextData)]) {
		_reloading = YES;
		[refreshHeaderView setState:EGOOPullRefreshLoading];
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.2];
		scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 60.0f, 0.0f);
		[UIView commitAnimations];
	}
	
	if ([MCUserDefaults instance].dragListLastAndMore) {
		if (totalPage > 1 && page > 0) {
			if (refreshHeaderView_Up == nil && scrollView == vLeft) {
				CGRect frame = CGRectMake(0.0f, -65, vLeft.bounds.size.width, 65.0);
				refreshHeaderView_Up = [[EGORefreshTableHeaderView alloc] initWithFrame:frame withPage:page withTotlePage:totalPage withFlag:NO];
				refreshHeaderView_Up.backgroundColor = [UIColor clearColor];
				[vLeft addSubview:refreshHeaderView_Up];
				[refreshHeaderView_Up release];
			}
		}
		if (scrollView.contentOffset.y <= - 65.0f && !_reloading_up && 
			[viewController respondsToSelector:@selector(getPreviousData)] &&
			[viewController performSelector:@selector(getPreviousData)]) {
			_reloading_up = YES;
			[refreshHeaderView_Up setState:EGOOPullRefreshLoading];
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDuration:0.2];
			scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
			[UIView commitAnimations];
		}
	}
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{	
	
	if (scrollView.isDragging) {
		int nScrollHeight = scrollView.bounds.size.height;
		if (refreshHeaderView.state == EGOOPullRefreshPulling && refreshHeaderView &&
			scrollView.contentOffset.y+nScrollHeight >= vLeft.contentSize.height && 
			scrollView.contentOffset.y+nScrollHeight < vLeft.contentSize.height+65 && !_reloading) {
			[refreshHeaderView setState:EGOOPullRefreshNormal];
		} else if (refreshHeaderView.state == EGOOPullRefreshNormal && refreshHeaderView &&
				   scrollView.contentOffset.y+nScrollHeight >= vLeft.contentSize.height+65 && !_reloading) {
			[refreshHeaderView setState:EGOOPullRefreshPulling];
		}
		
		if ([MCUserDefaults instance].dragListLastAndMore) {
			if (refreshHeaderView_Up.state == EGOOPullRefreshPulling && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !_reloading_up) {
				[refreshHeaderView_Up setState:EGOOPullRefreshNormal];
			} else if (refreshHeaderView_Up.state == EGOOPullRefreshNormal && scrollView.contentOffset.y < -65.0f && !_reloading_up) {
				[refreshHeaderView_Up setState:EGOOPullRefreshPulling];
			}
		}
	}
}

- (void)refreshFinished:(int)curPage {
	
	_reloading = NO;
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:.3];
	[vLeft setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
	[UIView commitAnimations];
	
	[refreshHeaderView setState:EGOOPullRefreshNormal];
	
	if ([MCUserDefaults instance].dragListLastAndMore) {
		_reloading_up = NO;
		[refreshHeaderView_Up setState:EGOOPullRefreshNormal];
	}
}

- (void) dealloc {
	refreshHeaderView = nil;
	refreshHeaderView_Up = nil;
	vLeft == nil;
	vRight == nil;
	vRightContent == nil;
	vLeftContent == nil;
	vTopLeft == nil;
	vTopRight == nil;
	[dataObjectName release];	
	[layoutName release];	
	[listData release];
	[listLayout release];
	[gridType release];
	[buttons release];
	[valueBufferArray release];
	[dataSource release];
	[super dealloc];
}

-(void)selectCell:(id)inSender {
	UIButton *button = (UIButton *)inSender;
	int row = button.tag;	
	if (row == -1) {
		if ([self.listData count] <= 0) {
			return;
		}
		bool isSelected = YES;
		if ([valueBufferArray count] > 0 && [valueBufferArray count] == [self.listData count]) {
			[valueBufferArray removeAllObjects];
			[button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
			isSelected = NO;
		}else {
			[valueBufferArray removeAllObjects];
			for (int i = 0; i < [self.listData count]; i++) {
				NSMutableDictionary *record = [self.listData objectAtIndex:i];
				[valueBufferArray addObject:record == nil?[NSMutableDictionary dictionary]:record];
			}
			[button setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
		}
		for (UIView *view in [vLeftContent subviews]) {
			if ([view isKindOfClass:[UIButton class]]) {
				UIButton *btn = (UIButton *)view;
				if (isSelected) {
					[btn setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
				}else {
					[btn setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
				}
			}
		}
		return;
	}
	
	NSMutableDictionary *record = [self.listData objectAtIndex:row - 100];
	bool selectFlag = YES;
	for (int i = 0; i < [valueBufferArray count]; i++) {
		NSMutableDictionary *recordValue = [valueBufferArray objectAtIndex:i];
		NSString *key = [recordValue objectForKey:@"key"];
		if (nil == key 
			|| [@"" isEqualToString:key] 
			|| nil == [record objectForKey:@"key"]
			|| [@"" isEqualToString:[record objectForKey:@"key"]]) {
			continue;
		}
		if ([[record objectForKey:@"key"] isEqualToString:key]) {
			[valueBufferArray removeObjectAtIndex:i];
			selectFlag = NO;
			break;
		}
	}
	if (selectFlag) {
		[button setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
		[valueBufferArray addObject:record == nil?[NSMutableDictionary dictionary]:record];
	}else {
		[button setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
	}
	UIButton *selectAll = (UIButton *)[vTopLeft viewWithTag:-1];
	if (selectAll && [valueBufferArray count] != [self.listData count]) {
		[selectAll setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
	}
}

-(void)doAction:(int)row
{
	actionSheetFlag = row;
	
	NSMutableDictionary *record = [self.listData objectAtIndex:row];
	NSMutableArray *dataButtons = [record objectForKey:@"btn"];
	NSMutableArray *cfgButtons = nil;
	
	if ([self.gridType isEqualToString:@"S"]) {
		NSMutableDictionary *subListConfig = [[listLayout objectForKey:@"slc"] objectAtIndex:tabIndex-1];
		if ([subListConfig objectForKey:@"dbtn"]) {
			cfgButtons =[subListConfig objectForKey: @"dbtn"];
		}	
	} else {	
		if ([listLayout objectForKey:@"dbtn"]) {
			cfgButtons = [listLayout objectForKey:@"dbtn"];
		}	
	}
	buttons = [[MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons] retain];
    
	if ([buttons count] <= 0) {
		return;
	}
	
	if ([buttons count] == 1) {
		NSString *pKey = @"";
		if ([record objectForKey:@"key"] != nil) {
			pKey = [record objectForKey:@"key"];
		}
		NSMutableDictionary *button = [buttons objectAtIndex:0];
		if ([MCErrorManagement showAuthMessage:button]) {
			return;
		}
		NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   buttonId,@"buttonId",
										   @"list_rec",@"buttonType",
										   self.dataObjectName,@"dataObjectName",
										   record,@"formData",
										   pKey, @"primaryKey", nil];
		
		if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
						   parameters:parameters 
					   confirmMessage:[button objectForKey:@"cfmi"] 
					   naviController:viewController.navigationController];
		}else{
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
						   parameters:parameters 
					   confirmMessage:nil 
					   naviController:viewController.navigationController];
		}
		[parameters release];
	} else if ([buttons count] > 1) {
		UIActionSheet *menu = [MCUIToolkit actionSheet:buttons
											   withTag:0
										  withDelegate:self
											 withTitle:@"请选择明细操作"];
		[menu showInView:viewController.clientView.window];
	}
}


//弹出菜单按钮点击事件，当actionSheetFlag等于-1的时候执行列表动作，否则执行列表明细动作
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == [buttons count]) {
		// 按下取消按钮
		return;
	}
	int row = actionSheetFlag;	
	NSMutableDictionary *record = [self.listData objectAtIndex:row];
	NSMutableDictionary *button = [buttons objectAtIndex:buttonIndex];
	if ([MCErrorManagement showAuthMessage:button]) {
		return;
	}
	NSString *pKey = @"";
	if ([record objectForKey:@"key"] != nil) {
		pKey = [record objectForKey:@"key"];
	}
	NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
	NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										buttonId,@"buttonId",
										@"list_rec",@"buttonType",
										self.dataObjectName,@"dataObjectName",
										record,@"formData",
									   pKey, @"primaryKey", nil];
	
	if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
		[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
					   parameters:parameters 
				   confirmMessage:[button objectForKey:@"cfmi"] 
				   naviController:viewController.navigationController];
	}else{
		[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
					   parameters:parameters 
				   confirmMessage:nil 
				   naviController:viewController.navigationController];
	}
	[parameters release];
}
@end
